﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace ado_test
{
    public partial class MainForm : Form
    {
        public LoginForm loginform;
        public string username;
        
        public MainForm()
        {
            InitializeComponent();
        }

        public MainForm(LoginForm l, string username) : this()
        {
            this.loginform = l;
            this.username = username;
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.Exit();
            loginform.Close();
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ModifyPasswd f = new ModifyPasswd();
            f.username = this.username;
            //f.MdiParent = this;
            f.ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“studentDataSet.StuInfo”中。您可以根据需要移动或删除它。
            this.stuInfoTableAdapter.Fill(this.studentDataSet.StuInfo);
            // TODO: 这行代码将数据加载到表“studentDataSet.StuInfo”中。您可以根据需要移动或删除它。
            this.stuInfoTableAdapter.Fill(this.studentDataSet.StuInfo);
             this.Text = "欢迎" + this.username;
        }

        private void 浏览信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.stuInfoTableAdapter.Update(this.studentDataSet.StuInfo);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.stuInfoBindingSource.Filter = "StuNo='" + this.textBox1.Text + "'";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.radioButton1.Checked == true)
            {
                this.stuInfoBindingSource.Filter = "StuName ='" + this.textBox2.Text + "'";
            }
            else
            {
                this.stuInfoBindingSource.Filter = "StuName like '*" + this.textBox2.Text + "*'";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.stuInfoBindingSource.MoveFirst();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.stuInfoBindingSource.MoveNext();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.stuInfoBindingSource.MovePrevious();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.stuInfoBindingSource.MoveLast();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //this.dataGridView1.DataSource = this.stuInfoBindingSource;
            this.stuInfoTableAdapter.Fill(this.studentDataSet.StuInfo);
        }

        private void 导出数据到文本文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = this.saveFileDialog1.FileName;
                FileStream fs = File.Open(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=student.accdb";
                conn.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandText = "Select * from StuInfo";
                OleDbDataReader reader = cmd.ExecuteReader();

                int n = 0;
                while (reader.Read())
                {
                    string s =  reader[0].ToString() + "\t" +
                                reader[1].ToString() + "\t" +
                                reader[2].ToString() + "\t" +
                                reader[3].ToString() + "\t" +
                                reader[4].ToString();
                    sw.WriteLine(s);
                    n++;
                }

                reader.Close();
                conn.Close();
                sw.Close();
                fs.Close();

                MessageBox.Show("已保存到文件" + filename + "，总共" + n + "条记录");

            }
        }

        private void 从文本文件导入数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = this.openFileDialog1.FileName;
                FileStream fs = File.Open(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs, Encoding.Default);

                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=student.accdb";
                conn.Open();


                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandText = "insert into StuInfo values(?,?,?,?,?)";
                cmd.Parameters.Add("@StuNo", OleDbType.Char);
                cmd.Parameters.Add("@StuName", OleDbType.Char);
                cmd.Parameters.Add("@StuAge", OleDbType.Numeric);
                cmd.Parameters.Add("@StuSex", OleDbType.Char);
                cmd.Parameters.Add("@StuProvince", OleDbType.Char);

                OleDbCommand cmd2 = new OleDbCommand();
                cmd2.Connection = conn;
                cmd2.CommandText = "select count(*) from StuInfo where StuNo=?";
                cmd2.Parameters.Add("@StuNo", OleDbType.Char);

                int count = 0;
                while (!sr.EndOfStream)
                {
                    string s = sr.ReadLine();
                    MessageBox.Show(s);
                    string[] s2 = s.Split('\t');

                    cmd2.Parameters["@StuNo"].Value = s2[0];
                    int n = (int)cmd2.ExecuteScalar();
                    MessageBox.Show(n.ToString());
                    if (n == 0)
                    {
                        cmd.Parameters[0].Value = s2[0];
                        cmd.Parameters[1].Value = s2[1];
                        cmd.Parameters[2].Value = decimal.Parse(s2[2]);
                        cmd.Parameters[3].Value = s2[3];
                        cmd.Parameters[4].Value = s2[4];
                        int i = cmd.ExecuteNonQuery();
                        MessageBox.Show(i.ToString());
                        count++;
                    }
                }

                MessageBox.Show("已导入" + count + "条记录！");
                sr.Close();
                fs.Close();
                conn.Close();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void 添加新用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
